package  com.obas.business.analogy.view {
//   import com.as3xls.xls.ExcelFile;
//   import com.as3xls.xls.Sheet;
   import com.ease.common.vo.Pagination;
   import com.ease.core.events.ViewOperationEvent;
   import com.ease.framework.CommandCompletionEvent;
   import com.ease.component.managers.ConfirmHelper;
   import com.ease.component.managers.ErrorManager;
   import com.ease.component.managers.ProgressManager;
   import com.ease.framework.CommandCompletionEvent;
   import com.obas.business.analogy.vo.AnalogyParam;
   import com.obas.business.customer.dialog.CreateCustomerDialog;
   import com.obas.business.customer.event.CustomerManagementEvent;
   import com.obas.business.topo.event.TopoVisualizationEvent;
   import com.obas.common.event.DimensionOperationEvent;
   import com.obas.common.util.ResourceUtil;
   
   import flash.events.Event;
   import flash.events.EventDispatcher;
   import flash.events.KeyboardEvent;
   import flash.events.MouseEvent;
   import flash.net.URLRequest;
   import flash.net.navigateToURL;
   import flash.system.System;
   import flash.ui.Keyboard;
   import flash.utils.ByteArray;
   
   import mx.collections.ArrayCollection;
   import mx.collections.IList;
   import mx.controls.Alert;
   import mx.controls.advancedDataGridClasses.AdvancedDataGridColumn;
   import mx.events.CloseEvent;
   import mx.events.ItemClickEvent;
   import mx.events.MenuEvent;
   import mx.managers.PopUpManager;
   import mx.utils.ObjectUtil;
   import mx.utils.StringUtil;

   [Event(name="{com.ease.core.events.ViewOperationEvent.REQUEST_ID}", type="com.ease.core.events.ViewOperationEvent")]
   
   
   [Event(name="{com.obas.common.event.DimensionOperationEvent.REQUEST_ID}",
         type="com.obas.common.event.DimensionOperationEvent")]
   
   
   public class UserBusinessExperienceUserGridMediator extends EventDispatcher {


      private var _view:UserBusinessExperienceUserGridView;
	  
      [InjectableView]     
      public function get view():UserBusinessExperienceUserGridView {
         return _view;
      }

      public function set view(value:UserBusinessExperienceUserGridView):void {
         _view = value;
		 _view.viewByIndicator = viewByIndicator;
         init();
		 onRefresh();
      }

      private function init():void {
         appendEventListeners();
         
	  }
	 
	  
      private function appendEventListeners():void {
//		  _view.indicatorTypeToggleButtonBar.addEventListener(ItemClickEvent.ITEM_CLICK, onIndicatorTypeToggleButtonBarItemClick);
		  _view.timeDimensionTypeToggleButtonBar.addEventListener(ItemClickEvent.ITEM_CLICK, onTimeDimensionTypeToggleButtonBarItemClick);
		  
		 /* _view.viewBtn.addEventListener(MouseEvent.CLICK, onView);*/
		  _view.refreshBtn.addEventListener(MouseEvent.CLICK, onRefresh);
		  
		
      }
      
	  
	  private function onView(e:*):void {
		  		  
		  if(_view.datagrid.selectedItem) {
//			  var topoVisualizationEvent:TopoVisualizationEvent=new TopoVisualizationEvent(TopoVisualizationEvent.OPERATION_VIEW_USER_ROUTE);
//			  topoVisualizationEvent.accountId=_view.datagrid.selectedItem.customerId;
//			  //			topoVisualizationEvent.devices=new ArrayCollection(_view.datagrid.selectedItems);
//			  dispatchEvent(topoVisualizationEvent);
			  
			  var param:Object = new Object();
			  param.targetView = "userBusinessExperienceObjectView";
			  param.className = "com.obas.business.analogy.objectview.UserBusinessExperienceObjectView";
			  param.createNew = "true";
			  param.autoOpen = "true";
//			  var entity:Object = new Object();
//			  param.entity =  entity;

//			  entity.customerId = _view.datagrid.selectedItem
			  param.entity =_view.datagrid.selectedItem;
//			  entity.customerId = _view.datagrid.selectedItem.customerId;
//			  entity.indicatorTypeId = _view.datagrid.selectedItem.indicatorTypeId;
//			  entity.networkServingId = _view.datagrid.selectedItem.networkServingId;
//			  entity.appIdentity = _view.datagrid.selectedItem.appIdentity;
			  
  			  dispatchEvent(new ViewOperationEvent([param.targetView], param));
			  
		  }
		  
	  }
	  
	  private function onRefresh(e:*=null):void {
		  ProgressManager.showProgress(_view.datagrid);
//		var indicatorTypes:IList =_view.indicatorTypeToggleButtonBar.dataProvider as IList;
//		var indicatorType:Object =   indicatorTypes.getItemAt(_view.indicatorTypeToggleButtonBar.selectedIndex);
		//
		var timeDimensionTypes:IList =_view.timeDimensionTypeToggleButtonBar.dataProvider as IList;
		var timeDimensionType:Object =   timeDimensionTypes.getItemAt(_view.timeDimensionTypeToggleButtonBar.selectedIndex);
		
		var dimensionOperationEvent:DimensionOperationEvent = new DimensionOperationEvent(DimensionOperationEvent.OPERATION_findUserBusinessExperienceUserGrid);
		
		var analogyParam:AnalogyParam=new AnalogyParam();
//		analogyParam.indicatorTypeCode=indicatorType.code;
		analogyParam.timeDimensionTypeCode=timeDimensionType.code;
		
		dimensionOperationEvent.analogyParam=analogyParam;
		
		dispatchEvent(dimensionOperationEvent);
		
	  }
	  
	  public function viewByIndicator(object:Object, indicatorTypeCode:String): void{
				  
				  var param:Object = new Object();
				  param.targetView = "userBusinessExperienceObjectView";
				  param.className = "com.obas.business.analogy.objectview.UserBusinessExperienceObjectView";
				  param.createNew = "true";
				  param.autoOpen = "true";
				  
				  var entity:Object = new Object();
				  param.entity =  entity;
				  
				  entity.customerId =object.customerId;
				  entity.networkServingId =object.networkServingId;
				  entity.indicatorTypeCode=indicatorTypeCode;
//				  entity.appIdentity=object.appIdentity;
				  
				  dispatchEvent(new ViewOperationEvent([param.targetView], param));
				  
					  
		  }
	  
	  private function onTimeDimensionTypeToggleButtonBarItemClick(e:ItemClickEvent):void {
		  onRefresh();
	  }
	  
	  private function onIndicatorTypeToggleButtonBarItemClick(e:ItemClickEvent):void {
		  onRefresh();
//		  myTA.text="Selected button index: " + String(event.index) +
//			  "\n" + "Selected button label: " + event.label;
	  }
	  
	  [ResponseHandler("{com.obas.common.event.DimensionOperationEvent.RETURN_ID}")]
	  public function onDimensionOperationEvent(event:CommandCompletionEvent):void
	  {
		  //		  ProgressManager.hideProgress(_view);
		  var request:DimensionOperationEvent=event.request as DimensionOperationEvent;
		  if (event.error)
		  {
			  ErrorManager.showErrorMessage(ResourceUtil.getErrorString("", event.error));
			  return;
		  }
		  
		  switch (request.actionType)
		  {
			  case DimensionOperationEvent.OPERATION_findUserBusinessExperienceUserGrid:
				  ProgressManager.hideProgress(_view.datagrid);
				  _view.dataProvider=event.result  as ArrayCollection;
				  break;
			
			  //              case DataRefreshEvent.TYPE_ADD_UPDATE :
			  //                 addOrUpdateItems(event.spec.data);
			  //                 break;
			  
		  }
		  
	  }
   }
}